<template>
  <div class="upload">
    <div class="img-wrapper el-icon-picture" :class="className">
      <input type="file" name="file" class="upload__input" @change="handleChange">
      <img :src="imageUrl" alt>
      <div v-if="imageUrl" class="filter">
        <i class="el-icon-zoom-in" :class="[small, medium]" @click="handleZoomIn" />
        <slot />
        <i class="el-icon-delete" :class="[small, medium]" @click="handleRemove" />
      </div>
    </div>
    <div v-if="title" class="title">{{ title }}</div>
    <div slot="tip" class="el-upload__tip color-red">{{ tip }}</div>
    <el-dialog :visible.sync="imgDialogVisible">
      <img width="100%" :src="imageUrl" alt>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios'
const ERR_OK = 200
export default {
  props: {
    small: {
      type: String,
      default: ''
    },
    medium: {
      type: String,
      default: ''
    },
    isIcon: {
      type: Boolean,
      default: true
    },
    tip: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    minHeight: {
      type: Number,
      default: 0
    },
    minWidth: {
      type: Number,
      default: 0
    },
    width: {
      type: Number,
      default: 0
    },
    height: {
      type: Number,
      default: 0
    },
    className: {
      type: String,
      default: 'normal'
    },
    img: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imageUrl: '',
      imgDialogVisible: false
    }
  },
  watch: {
    img: {
      immediate: true,
      handler(newVal) {
        this.imageUrl = newVal
      }
    }
  },
  created() {
    this.imgUrl = this.img
  },
  mounted() {
    const $doms = document.querySelectorAll(`.${this.className}`)
    if (this.minHeight > 0) {
      for (let i = 0; i < $doms.length; i++) {
        $doms[i].style.minHeight = this.minHeight + 'px'
      }
    }
    if (this.minWidth > 0) {
      for (let i = 0; i < $doms.length; i++) {
        $doms[i].style.minWidth = this.minWidth + 'px'
      }
    }
    if (this.width > 0) {
      for (let i = 0; i < $doms.length; i++) {
        $doms[i].style.width = this.width + 'px'
      }
    }
    if (this.height > 0) {
      for (let i = 0; i < $doms.length; i++) {
        $doms[i].style.height = this.height + 'px'
      }
    }
  },
  methods: {
    handleZoomIn() {
      this.$emit('handleZoomIn')
      this.imgDialogVisible = true
    },
    handleRemove() {
      setTimeout(() => {
        this.$emit('remove', this.imageUrl)
        this.imageUrl = ''
      }, 20)
    },
    handleChange(e) {
      const file = e.target.files[0]
      const formData = new FormData()
      formData.append('photo', file)
      const baseUrl = process.env.VUE_APP_BASE_API
      axios({
        url: `${baseUrl}/api/common/upload`,
        method: 'post',
        data: formData
      }).then(res => {
        console.log(res)
        if (res.data.code === ERR_OK) {
          this.imageUrl = res.data.data.photo
          this.$emit('success', res.data.data.photo)
        }
      })
    },
    clear() {
      this.imageUrl = ''
    }
  }
}
</script>
<style lang="scss" scoped>
.color-red {
  color: red;
}
.el-upload__tip{
  height: 20px;
  line-height: 20px;
  margin: 0;
}
.upload {
   display: flex;
   flex-direction: column;
   align-items: center;
  .img-wrapper {
    text-align: center;
    position: relative;
    cursor: pointer;
    overflow: hidden;
    background-color: rgba(245, 245, 245, 1);
    border: 1px solid rgba(255, 255, 255, 0);
    img {
      width: 100%;
      position: relative;
      z-index: 99;
      vertical-align: bottom;
    }
    .filter {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      z-index: 100;
      display: flex;
      align-items: center;
      justify-content: center;
      .el-icon-zoom-in,
      .el-icon-delete {
        font-size: 32px;
        color: #fff;
        opacity: 0;
        &.small{
          margin-right: 0px !important;
          font-size: 16px !important;
        }
        &.medium{
          font-size: 20px !important;
        }
      }
      .el-icon-zoom-in {
        margin-right: 15px;
      }
      &:hover {
        opacity: 0.6;
        transition: all 0.5s;
        background-color: rgba(0, 0, 0, 1);
        .el-icon-zoom-in,
        .el-icon-delete {
          opacity: 1;
          transform: all 0.5s;
          z-index: 101;
        }
      }
    }
    .title{
      color: #000;
      text-align: center;
      font-size: 14px;
    }
    &.el-icon-picture:before {
      display: inline-block;
      width: 45px;
      height: 45px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      font-size: 45px;
      color: #ccc;
    }
    .upload__input {
      opacity: 0;
      overflow: visible;
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      cursor: pointer;
    }
  }
}
</style>
